package com.framework.modules.job.task;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.entity.flow.FlowAccount;
import com.framework.entity.flow.FlowFrozenTerminal;
import com.framework.service.AgentInfoService;
import com.framework.service.FlowAccountService;
import com.framework.service.FlowFrozenTerminalService;
import com.framework.service.TransactionInfoService;
import com.framework.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通联解冻任务
 *
 * @author wq
 */
@Component("tLThawFlowTask")
@Slf4j
public class TlThawFrozenTask {


    @Autowired
    private FlowFrozenTerminalService flowFrozenTerminalService;

    @Autowired
    private TransactionInfoService transactionInfoService;

    @Autowired
    private FlowAccountService flowAccountService;

    @Autowired
    private AgentInfoService agentInfoService;

    /**
     * 解除冻结
     */
    @Transactional(rollbackFor = Exception.class)
    public void thawFrozen() {

        log.info("通联解冻任务开始");
        List<FlowFrozenTerminal> terminals = flowFrozenTerminalService.selectList(
                new EntityWrapper<FlowFrozenTerminal>().eq("dept_id", 5)
                        .gt("amount",0)
                .eq("thaw_flag",1)
        );

        for (FlowFrozenTerminal terminal : terminals) {
            //查询交易额达标的机器
            List<String> list = transactionInfoService.queryTranAndSumList(terminal.getSn());
            //转换格式
            List<Date> dates = list.stream().map(e -> DateUtils.stringToDate(e,"yyyy-MM")).collect(Collectors.toList());

            //至少需要3个月的交易额
            if (list.size() >= 3) {
                log.info("解冻sn:"+terminal.getSn()+"中...");
                Date temp;

                int count=0;

                for (int i = terminal.getState(); i < dates.size() - 1; i++) {
                    Date date = dates.get(i);

                    temp = DateUtils.addDateMonths(date, 1);

                    if (temp.compareTo(dates.get(i + 1)) == 0) {
                        count++;
                    } else {
                        count = 1;
                    }
                    if (count == 2) {
                        thawFrozenToAgent(terminal);
                        log.info("解冻sn:"+terminal.getSn()+"完成");
                        break;
                    }
                }
            }
        }
        log.info("通联解冻任务开始");
    }

    /**
     * 解冻金额给合伙人
     * @param terminal
     */
    private void thawFrozenToAgent(FlowFrozenTerminal terminal) {
        agentInfoService.thawAmount(terminal.getAgentId(),new BigDecimal("100"));
        BigDecimal subtract = terminal.getAmount().subtract(new BigDecimal("100"));
        FlowAccount flowAccount = new FlowAccount();

        flowAccount.setAgentId(terminal.getAgentId());
        flowAccount.setCreateTime(new Date());
        flowAccount.setSupportType(2);
        flowAccount.setAmount(new BigDecimal("100"));
        flowAccount.setAccountType(6);
        flowAccount.setBelId(terminal.getSn());
        flowAccount.setRemark("交易达标,解冻奖励");
        flowAccountService.insert(flowAccount);


        terminal.setState(1);
        terminal.setThawTime(new Date());
        terminal.setAmount(subtract);
        flowFrozenTerminalService.updateById(terminal);

    }


    public static void main(String[] args) {
        List<String> list= Arrays.asList("2018-06","2018-07","2018-08","2018-09");


        List<Date> dates = list.stream().map(e -> DateUtils.stringToDate(e, "yyyy-MM")).collect(Collectors.toList());
        Date temp;
        int count=0;

        for (int i = 0; i < dates.size() - 1; i++) {

            Date date = dates.get(i);
            System.out.println("当前时间"+date);
            temp = DateUtils.addDateMonths(date, 1);
            System.out.println("增加后时间"+date);
            if (temp.compareTo(dates.get(i + 1)) == 0) {
                count++;
            } else {
                count = 1;
            }
            System.out.println(count);
            if (count == 2) {
                System.out.println(DateUtils.format(dates.get(i + 1),DateUtils.DATE_PATTERN));
                System.out.println("解冻成功");
                break;
            }
        }


    }

}
